package com.industrial.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;

import com.industrial.app.domain.BizGycpcxl;
import com.industrial.app.excel.entity.BizGycpcxlExcelEntity;
import com.industrial.app.mapper.BizGycpcxlFormalMapper;
import com.industrial.app.mapper.BizGycpcxlMapper;
import com.industrial.app.service.IBizGycpcxlService;
import com.industrial.common.core.utils.DateUtils;
import com.industrial.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 工业产品销售率Service业务层处理
 *
 * @author SangGuangCan
 * @date 2024-01-24
 */
@Service
public class BizGycpcxlServiceImpl implements IBizGycpcxlService
{
    @Autowired
    private BizGycpcxlMapper bizGycpcxlMapper;

    @Autowired
    private BizGycpcxlFormalMapper bizGycpcxlFormalMapper;

    /**
     * 查询工业产品销售率
     *
     * @param baseId 工业产品销售率主键
     * @return 工业产品销售率
     */
    @Override
    public BizGycpcxl selectBizGycpcxlByBaseId(String baseId)
    {
        return bizGycpcxlMapper.selectBizGycpcxlByBaseId(baseId);
    }

    /**
     * 查询工业产品销售率列表
     *
     * @param bizGycpcxl 工业产品销售率
     * @return 工业产品销售率
     */
    @Override
    public List<BizGycpcxl> selectBizGycpcxlList(BizGycpcxl bizGycpcxl)
    {
        return bizGycpcxlMapper.selectBizGycpcxlList(bizGycpcxl);
    }

    /**
     * 新增工业产品销售率
     *
     * @param bizGycpcxl 工业产品销售率
     * @return 结果
     */
    @Override
    public int insertBizGycpcxl(BizGycpcxl bizGycpcxl)
    {
        return bizGycpcxlMapper.insertBizGycpcxl(bizGycpcxl);
    }

    /**
     * 修改工业产品销售率
     *
     * @param bizGycpcxl 工业产品销售率
     * @return 结果
     */
    @Override
    public int updateBizGycpcxl(BizGycpcxl bizGycpcxl)
    {
        return bizGycpcxlMapper.updateBizGycpcxl(bizGycpcxl);
    }

    /**
     * 批量删除工业产品销售率
     *
     * @param baseIds 需要删除的工业产品销售率主键
     * @return 结果
     */
    @Override
    public int deleteBizGycpcxlByBaseIds(String[] baseIds)
    {
        return bizGycpcxlMapper.deleteBizGycpcxlByBaseIds(baseIds);
    }

    /**
     * 删除工业产品销售率信息
     *
     * @param baseId 工业产品销售率主键
     * @return 结果
     */
    @Override
    public int deleteBizGycpcxlByBaseId(String baseId)
    {
        return bizGycpcxlMapper.deleteBizGycpcxlByBaseId(baseId);
    }

    /**
     * @param absoluteExcelPath
     * @param yearMonth
     */
    @Override
    public void importExcel(String absoluteExcelPath, String yearMonth) {
        List<BizGycpcxlExcelEntity> excelDataArgs = EasyExcel.read(absoluteExcelPath).head(BizGycpcxlExcelEntity.class).headRowNumber(1).sheet().doReadSync();
        List<BizGycpcxl> list = new ArrayList<>();
        for(BizGycpcxlExcelEntity item : excelDataArgs){
            BizGycpcxl tem = new BizGycpcxl();
            BeanUtil.copyProperties(item,tem);
            String yue = StrUtil.removeSuffix(item.getMoths(),"月");
            tem.setMoths(yearMonth + "-" + String.format("%02d",Integer.valueOf(yue)));
            tem.setBaseCreateTime(DateUtils.getNowDate());
            tem.setBaseCreateBy(String.valueOf(SecurityUtils.getUserId()));
            tem.setBaseId(IdUtil.simpleUUID());
            list.add(tem);
        }
        if(!list.isEmpty()){
            bizGycpcxlMapper.batchInsertBizGycpcxl(list);
        }
    }

    /**
     *
     */
    @Override
    public void clearAllTableData() {
        bizGycpcxlMapper.clearAllTableData();
    }

    /**
     * @return
     */
    @Override
    public boolean transferDataInOneClick() {
        try{
            List<BizGycpcxl> result = bizGycpcxlMapper.selectBizGycpcxlAllData();

            int count = 0;
            List<BizGycpcxl> list = new ArrayList<>();
            for(BizGycpcxl item : result){
                list.add(item);
                count++;
                if(count >= 100){
                    bizGycpcxlFormalMapper.batchInsertBizGycpcxlFormal(list);
                    list.clear();
                }
            }

            if(!list.isEmpty()){
                bizGycpcxlFormalMapper.batchInsertBizGycpcxlFormal(list);
                list.clear();
            }
            return true;
        }catch (Exception e){
            System.out.println(e.getMessage());
            return false;
        }
    }
}
